#include "Entity.h"
#include "Constants.h"
#include "RessourceManager.h"

#include <iostream>

Entity::Entity()
{
    x = 0;
    y = 0;
    motionX = 0;
    motionY = 0;

    data = new EntityData;
    data->mass = 0;
    data->speed = 0;

    needsRemoval = false;
}

void Entity::tick()    // Calculate the next position of this entity.
{                               // That means gravity and everything is calculated for this entity.
    motionY -= 0.01f;
    motionX /= 1.2f;
}

void Entity::updateSprite()
{
    sprite.setPosition(x*TILE_SIZE, -y*TILE_SIZE-sprite.getGlobalBounds().height);
}

bool Entity::collide(Entity* entity)
{
    return getAbsoluteBoundingBox().intersects(entity->getAbsoluteBoundingBox());
}

float Entity::getX()
{
    return x;
}

float Entity::getY()
{
    return y;
}

float Entity::getMotionX()
{
    return motionX;
}

float Entity::getMotionY()
{
    return motionY;
}

AABB Entity::getBoundingBox()
{
    return data->aabb;
}

AABB Entity::getAbsoluteBoundingBox()
{

    return AABB(x, y, data->aabb.width, data->aabb.height);
}

sf::Sprite* Entity::getSprite()
{
    return &sprite;
}

void Entity::setX(float x)
{
    this->x = x;
}

void Entity::setY(float y)
{
    this->y = y;
}

void Entity::setMotionX(float motionX)
{
    this->motionX = motionX;
}

void Entity::setMotionY(float motionY)
{
    this->motionY = motionY;
}

void Entity::onCollide(Entity* other)
{

}

void Entity::takeHit(int damages, Entity* other)
{

}

void Entity::pickup(ItemEntity* item)
{

}

void Entity::setRemove()
{
    needsRemoval = true;
}

bool Entity::doesNeedRemoval()
{
    return needsRemoval;
}

Entity::~Entity()
{

}

// FACTORY

Entity* Entity::setSprite(std::string s)
{
    this->sprite.setTexture(*RessourceManager::getTexture(s));
    return this;
}

Entity* Entity::setSpeed(float f)
{
    data->speed = f;
    return this;
}

Entity* Entity::setMass(float f)
{
    data->mass = f;
    return this;
}

Entity* Entity::setAABB(float w,float h)
{
    return setAABB(0,0,w,h);
}

Entity* Entity::setAABB(float x, float y, float w, float h)
{
    data->aabb = AABB(x,y,w,h);
    return this;
}
